<?php
defined('BASEPATH') or exit('No direct script access allowed');

class StatisticsAttended extends AdminController
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Attend_model');
		$this->load->model("Admin_model");
		$this->load->model("Class_model");
		$this->load->library('ProjectService/admin_service');
	}

	function Index()
	{
		//按教师分类
		// 控制筛选项部门
		$deptlist=(new AdminDeptList)->getRemark()[0];
		for ($i=0; $i <count($deptlist) ; $i++) { 
			$deptlist[$i]='"'.$deptlist[$i].'"';
		}
		$connar['items'] = 'distinct(dept_name)';
		$connar['where']="dept_name in(".implode(',',$deptlist).")";
		$dept_name = $this->Admin_model->get_list_full($connar);

		$new_dept_name=array();
        foreach($dept_name as $k=>$v){
            if($v['dept_name']){
                $name = mb_substr($v['dept_name'],0,2,"utf-8");             
                if($name =='财经' ||$name =='商贸'||$name =='交通'||$name =='鉴湖'||$name =='黄酒'){
                    $new_dept_name[$k]['name'] = mb_substr($v['dept_name'],0,2,"utf-8");             
                }else if($name =='建筑' ||$name =='机电'||$name =='交通'){
                    $new_dept_name[$k]['name'] = mb_substr($v['dept_name'],0,4,"utf-8");   
                }else if($name =='设计'){
                    $new_dept_name[$k]['name'] = mb_substr($v['dept_name'],0,5,"utf-8");   
                }else{
                    $new_dept_name[$k]['name'] = $v['dept_name'];
                }
                 $new_dept_name[$k]['dept_name'] = $v['dept_name'];
            }
            

        }

        $data['dept_name']= $new_dept_name;

		/*//按开课单位分类
		$connar1['items'] = 'distinct(course_units)';
		$connar1['where'] = 'course_units is not null';

		$course_units = $this->Class_model->get_list_full($connar1);
		$course_units1 = array();
		foreach($course_units as $k1=>$v1){
			if($v1['course_units']){
				$course_units1[$k1]['course_units']=$v1['course_units'];

			}
		}

		// $data['dept_name'] = $dept_name;
		$data['course_units'] = $course_units1;*/

		 //获取开课单位
         $data['course_units'] = (new AdminDeptList)->getRemark()[1]; 

		$role=explode(',',getSess()['sub_role_id']);
		array_push($role,getSess()['role_id']);	
		if(in_array(WebRole::Admin[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)||in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)||in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)||in_array(WebRole::StudentInfoOfficer[XPHEnum::Value], $role)||in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value], $role)||in_array(WebRole::UnitLeader[XPHEnum::Value], $role)||in_array(WebRole::SupervisorDirector[XPHEnum::Value], $role)||in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)||in_array(WebRole::Manage[XPHEnum::Value], $role)||in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)||in_array(WebRole::MajorUnitLeader[XPHEnum::Value], $role)){
			$data['is_show'] = 1;
		}else{
			$data['is_show'] = 0;
		}	

		$this->load->view('admin/statisticsAttendedList', $data);
	}

	function getDatas($params = array(), $isExport = false)
	{
		$connar = array();
		$params = $_POST;
		if (!$isExport) {

			$connar = $this->Attend_model->pickPages($connar, $params);
		}

		$manage = $this->admin_service->getAdminById(getSess()['id']);

		//查询当前用户的角色
		$sub_role = explode(',',  $manage['sub_role_id']);
		array_push($sub_role,  $manage['role_id']);
		$sub_role=array_filter($sub_role);
		$sub_role=array_merge($sub_role);

		$dept_name = mb_substr(getSess()['dept_name'],0,2,"utf-8");             
        if($dept_name =='财经' ||$dept_name =='商贸'||$dept_name =='交通'||$dept_name =='鉴湖'||$dept_name =='黄酒'){
            $dept_name_1 = mb_substr(getSess()['dept_name'],0,2,"utf-8");             
        }else if($dept_name =='建筑' ||$dept_name =='机电'||$dept_name =='交通'){
            $dept_name_1  = mb_substr(getSess()['dept_name'],0,4,"utf-8");   
        }else if($dept_name =='设计'){
            $dept_name_1 = mb_substr(getSess()['dept_name'],0,5,"utf-8");   
        }else{
            $dept_name_1  =getSess()['dept_name'];
        }
		
		

		//判断是否管理员
	/*	if (!in_array(WebRole::Admin[XPHEnum::Value], $sub_role) && !in_array(WebRole::Manage[XPHEnum::Value], $sub_role)) {

			//督导组长可查询本分院
			if (in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $sub_role)) {
				//查询该用户的开课单位信息
				$connar5['items'] = 'distinct(course_units)';
				$connar5['where'] = $this->sqlEscape('teacher_code=?? or dept_id=??', array($manage['usercode'], getSess()['dept_id']));
				$connar5['join'] = array('db_admin', 'db_attend.teacher_code=db_admin.usercode');
				$dept_name_1 = $this->Attend_model->get_single_full($connar5);

				$params['course_units'] = $dept_name_1['course_units'];
				//$params['dept_name'] =  $manage['dept_name'];
			} else {
				$connar5['items'] = 'distinct(course_units)';
				$connar5['where'] = $this->sqlEscape('teacher_code=??', array($manage['usercode']));
				$dept_name_1 = $this->Attend_model->get_single_full($connar5);

				$params['course_units'] = $dept_name_1['course_units'];
			//	$params['dept_name'] =  $manage['dept_name'];
			}

			// pri($params['course_units']);

			//校督导,二级督导,外聘督导只能查看自己
			if (in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $sub_role) or in_array(WebRole::UnitSupervisor[XPHEnum::Value], $sub_role) or in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $sub_role)) {
				$params['teacher_code'] =  $manage['usercode'];
				if (in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $sub_role)) { //督导组长查询本分院
					unset($params['teacher_code']);
				}
			}
		}*/


		// 按开课单位分类
		if (isset($_POST['type']) && $_POST['type'] == 1) {
			$connar['where'] = $this->sqlEscape('db_attend.status=??', array(AttendStatus::Submit[XPHEnum::Value]));
			$where = $this->sqlEscape('where db_attend.status=?? ', array(AttendStatus::Submit[XPHEnum::Value]));
			if (isset($params['start_time']) && isset($params['end_time'])) {
				//$connar['where'].=' and db_attend.listen_time>='.strtotime($params['start_time']).' and db_attend.listen_time<='.strtotime($params['end_time'].'23:59:59');
				//$where.=' and db_attend.listen_time>='.strtotime($params['start_time']).' and db_attend.listen_time<='.strtotime($params['end_time'].'23:59:59');
			} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
				//$connar['where'].=' and db_attend.listen_time>='.strtotime($_POST['start_time']).' and db_attend.listen_time<='.strtotime($_POST['end_time'].'23:59:59');
				//$where.=' and db_attend.listen_time>='.strtotime($_POST['start_time']).' and db_attend.listen_time<='.strtotime($_POST['end_time'].'23:59:59');
			}

			//课程类型

			if (isset($params['class_type']) && $params['class_type'] > -1) {
				$connar['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				$where .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
			}



			//分院名称
			if (isset($params['course_units']) && $params['course_units'] > -1) {

				$name = mb_substr($params['course_units'],0,2,"utf-8");             
                if($name =='财经' ||$name =='商贸'||$name =='交通'||$name =='鉴湖'||$name =='黄酒'){
                    $params['course_units'] = mb_substr($params['course_units'],0,2,"utf-8");             
                }else if($name =='建筑' ||$name =='机电'||$name =='交通'){
                  $params['course_units'] = mb_substr($params['course_units'],0,4,"utf-8");   
                }else if($name =='设计'){
                    $params['course_units']= mb_substr($params['course_units'],0,5,"utf-8");   
                }else{
                    $params['course_units'] =$params['course_units'];
                }

				 $connar['where'].= " and  db_attend.course_units like '%".$params['course_units']."%'";
				 $where.= " and  db_attend.course_units like '%".$params['course_units']."%'";

				// $connar['where'] .= $this->sqlEscape(" and db_attend.course_units=?? ", array($params['course_units']));
				// $where .= $this->sqlEscape(" and  db_attend.course_units=?? ", array($params['course_units']));
			}

			//二级督导，校督导，外聘督导只能查看自己
			if (isset($params['teacher_code']) &&  $params['teacher_code'] != '') {
				$connar['where'] .= $this->sqlEscape(" and db_attend.teacher_code=?? ", array($params['teacher_code']));
				$where .= $this->sqlEscape(" and db_attend.teacher_code=?? ", array($params['teacher_code']));
			}

			$role=explode(',',getSess()['sub_role_id']);
			array_push($role,getSess()['role_id']);		
			if(in_array(WebRole::Manage[XPHEnum::Value], $role)){//管理员,查询全部
				// 记得解开
				// $connar['where'].=$this->sqlEscape(' and admin_type=??',array(AdminType::Auth[XPHEnum::Value]));
			}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){ //二级督导组长查询本分院
				// if(str_replace('分', '学', getSess()['dept_name']) == '机电工程学院' || str_replace('分', '学', getSess()['dept_name']) == '机械工程学院'){
				// 	 $connar['where'].=$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));
				// 	 $where.=$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));				
				// }else{
					// 公共基础教学部，数据查找为公共基础教学部以及人文社科部
					if(strstr($dept_name_1,'公共')!==false){
						$connar['where'].= "and(  db_attend.course_units like '%". $dept_name_1."%' or db_attend.course_units like '%人文社科%')";
				    	$where.= " and (db_attend.course_units like '%". $dept_name_1."%' or db_attend.course_units like '%人文社科%')";
					}else{
						$connar['where'].= " and  db_attend.course_units like '%". $dept_name_1."%'";
				    	$where.= " and  db_attend.course_units like '%". $dept_name_1."%'";
					}
					// $connar['where'].=$this->sqlEscape(' and attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				 //     $where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				// }
				
			
			//二级督导，校级督导，外聘督导查看自己
			}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
				$connar['where'].=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
			}else{//其他也只查看自己
				$connar['where'].=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
			}
			

			$connar['items'] = "group_concat(DISTINCT id) attend_id,db_attend.course_units";
			$connar['groupby'] = "course_units";
			$connar['orderby'] = 'field(dept_name,"电气电子工程分院","机械工程分院","建筑工程分院","设计与艺术分院","财经分院","交通分院","商贸分院","鉴湖分院","人文社科部")';

			$count = $this->Attend_model->query('select count(*)count from (select count(*) from db_attend ' . $where . ' group by course_units order by field(dept_name,"电气电子工程分院","机械工程分院","建筑工程分院","设计与艺术分院","财经分院","交通分院","商贸分院","鉴湖分院","人文社科部")) count');
			$data['count'] = $count[0]['count'];
			if (isset($params['pageSize'])) {
				$data["totalPage"] = ceil($count[0]["count"] / $params['pageSize']);
				$data['pageNo'] = $params['pageNo'];
			} else {
				$data["totalPage"] = ceil($count[0]["count"] / 20);
				$data['pageNo'] = 1;
			}

			$data["ret"] = $this->Attend_model->get_list_full($connar);
			// echo $this->db->last_query();
			if($data["ret"]){
				$datalist = $this->getConType($data["ret"],1,$params,$_POST);	
			    $data["ret"] = $datalist;
			}
				
			// 按开课教师分类
		} else if (isset($_POST['type']) && $_POST['type'] == 2) {
			$where = 'where 1=1';
			$connar['where'] = $this->sqlEscape('db_attend.status=??', array(AttendStatus::Submit[XPHEnum::Value]));

			if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
				$connar['where'] .= ' and db_attend.listen_time>=' . strtotime($_POST['start_time']) . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . '23:59:59');
				$where .= ' and db_attend.listen_time>=' . strtotime($_POST['start_time']) . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . '23:59:59');
			}
			//课程类型
			if (isset($params['class_type']) && $params['class_type'] > -1) {
				$connar['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
			}

			//分院名称
			if (isset($params['dept_name']) && $params['dept_name'] > -1) {
				 $connar['where'].= " and  db_attend.dept_name like '%".$params['dept_name']."%'";
				 $where.= " and  db_attend.dept_name like '%".$params['dept_name']."%'";
				// $connar['where'].=$this->sqlEscape(' and db_attend.dept_name=??',array(str_replace('分', '学',$params['dept_name'])));
				// $where.=$this->sqlEscape(' and db_attend.dept_name=??',array(str_replace('分', '学',$params['dept_name'])));
				// $connar['where'] .= $this->sqlEscape(" and db_attend.dept_name=?? ", array($params['dept_name']));
			}

			//二级督导，校督导，外聘督导只能查看自己
			if (isset($params['teacher_code']) &&  $params['teacher_code'] != '') {
				$connar['where'] .= $this->sqlEscape(" and db_attend.teacher_code=?? ", array($params['teacher_code']));
			}
			$role=explode(',',getSess()['sub_role_id']);
			array_push($role,getSess()['role_id']);		
			if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				// 记得解开
				// $connar['where'].=$this->sqlEscape(' and admin_type=??',array(AdminType::Auth[XPHEnum::Value]));
			}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){				
				// if(str_replace('分', '学', getSess()['dept_name']) == '机电工程学院' || str_replace('分', '学', getSess()['dept_name']) == '机械工程学院'){
				// 	$connar['where'].=$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));
				// 	$where.$this->sqlEscape(' and (db_attend.course_units=?? or db_attend.course_units=??)',array('机电工程学院','机械工程学院'));
					
				// }else{
					// 公共基础教学部，数据查找为公共基础教学部以及人文社科部
					if(strstr($dept_name_1,'公共')!==false){
						$connar['where'].= "and(  db_attend.course_units like '%". $dept_name_1."%' or db_attend.course_units like '%人文社科%')";
				    	$where.= " and (db_attend.course_units like '%". $dept_name_1."%' or db_attend.course_units like '%人文社科%')";
					}else{
						$connar['where'].= " and  db_attend.course_units like '%". $dept_name_1."%'";
				    	$where.= " and  db_attend.course_units like '%". $dept_name_1."%'";
					}
					// $connar['where'].=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				 //    $where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				// }				
			
			}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
				$connar['where'].=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
			}else{
				$connar['where'].=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
			}

			if (isset($_POST['teacher_name'])) {
				$connar['where'] .= $this->sqlLikeEscape(" and teacher_name like '%??%'", array($_POST['teacher_name']));
				$where .= $this->sqlLikeEscape(" and teacher_name like '%??%'", array($_POST['teacher_name']));
			}

			$connar['items'] = " distinct(teacher_code) ,teacher_name";
			$connar['groupby'] = "teacher_code";
			$connar['orderby'] = 'listen_time desc';


			$count = $this->Attend_model->query('select count(*)count from (select ' . $connar['items'] . ' from db_attend where ' . $connar['where'] . ' group by teacher_code) count');
			$data['count'] = $count[0]['count'];
			if (isset($params['pageSize'])) {
				$data["totalPage"] = ceil($count[0]["count"] / $params['pageSize']);
				$data['pageNo'] = $params['pageNo'];
			} else {
				$data["totalPage"] = ceil($count[0]["count"] / 20);
				$data['pageNo'] = 1;
			}
			// pri($connar);
			$data["ret"] = $this->Attend_model->get_list_full($connar);
			// echo $this->db->last_query();

			if($data["ret"]){
				$datalist = $this->getConType($data["ret"],2,$params,$_POST);	
			    $data["ret"] = $datalist;
			}

			
		}

		if (!$isExport) {

			$attend_num_unit = 0;
			$attend_count_unit = 0;
			$attend_num_school = 0;
			$attend_count_school = 0;

			$attend_num_outside = 0;
			$attend_count_outside = 0;
				$attend_num_teacher = 0;
			$attend_count_teacher = 0;

			$attend_num_unitLeader = 0;
			$attend_count_unitLeader = 0;
			
			$attend_num_majorUnitLeader = 0;
			$attend_count_majorUnitLeader = 0;

			$avg_total_score = 0; //总平均分
			$avg_school_score = 0; //校督导平均分
			$avg_unit_score = 0; //二级督导平均分
			$avg_outside_score = 0; //外聘平均分
			$avg_teacher_score = 0; //教师平均分
			$avg_unitLeader_score = 0;//领导干部
			$avg_majorUnitLeader_score = 0;//领导干部

			// pri($datalist['ret']);

			foreach ($data['ret'] as $k => $v) {
				$attend_num_unit += $v['unitsupervisor_num'];
				$attend_count_unit += $v['unitsupervisor_count'];
				$attend_num_school += $v['schoolsupervisor_num'];
				$attend_count_school += $v['schoolsupervisor_count'];
				$attend_num_outside += $v['outsidesupervisor_num'];
				$attend_count_outside += $v['outsidesupervisor_count'];
				$attend_num_teacher += $v['teachersupervisor_num'];
				$attend_count_teacher += $v['teachersupervisor_count'];
				
				$attend_num_unitLeader += $v['unitLeadersupervisor_num'];
				$attend_count_unitLeader += $v['unitLeadersupervisor_count'];

				$attend_num_majorUnitLeader += $v['majorUnitLeadersupervisor_num'];
				$attend_count_majorUnitLeader += $v['majorUnitLeadersupervisor_count'];

				$avg_total_score += $v['avg_total_score'];
				$avg_school_score += $v['avg_school_score'];
				$avg_unit_score += $v['avg_unit_score'];
				$avg_outside_score += $v['avg_outside_score'];
				$avg_teacher_score += $v['avg_teacher_score'];
				$avg_unitLeader_score += $v['avg_unitLeader_score'];
				$avg_majorUnitLeader_score += $v['avg_majorUnitLeader_score'];
				$data['ret'][$k]['is_total'] = 0;
			}

			$count = count($data['ret']);

			if($data['ret']){
				$data['ret'][$count]['course_units'] = '合计';
				$data['ret'][$count]['unitsupervisor_num'] = $attend_num_unit;
				$data['ret'][$count]['unitsupervisor_count'] = $attend_count_unit;
				$data['ret'][$count]['schoolsupervisor_num'] = $attend_num_school;
				$data['ret'][$count]['schoolsupervisor_count'] = $attend_count_school;
				$data['ret'][$count]['outsidesupervisor_num'] = $attend_num_outside;
				$data['ret'][$count]['outsidesupervisor_count'] = $attend_count_outside;
				$data['ret'][$count]['teachersupervisor_num'] = $attend_num_teacher;
				$data['ret'][$count]['teachersupervisor_count'] = $attend_count_teacher;

				$data['ret'][$count]['unitLeadersupervisor_num'] = $attend_num_unitLeader;
				$data['ret'][$count]['unitLeadersupervisor_count'] = $attend_count_unitLeader;

				$data['ret'][$count]['majorUnitLeadersupervisor_num'] = $attend_num_majorUnitLeader;
				$data['ret'][$count]['majorUnitLeadersupervisor_count'] = $attend_count_majorUnitLeader;

				$data['ret'][$count]['avg_total_score'] = round(($avg_total_score/$count),2);
				$data['ret'][$count]['avg_school_score'] = round(($avg_school_score/$count),2);
				$data['ret'][$count]['avg_unit_score'] = round(($avg_unit_score/$count),2);
				$data['ret'][$count]['avg_outside_score'] =  round(($avg_outside_score/$count),2);
				$data['ret'][$count]['avg_teacher_score'] =  round(($avg_teacher_score/$count),2);

				$data['ret'][$count]['avg_unitLeader_score'] =  round(($avg_unitLeader_score/$count),2);
				$data['ret'][$count]['avg_majorUnitLeader_score'] =  round(($avg_majorUnitLeader_score/$count),2);

				$data['ret'][$count]['max_total_score'] = '';
				$data['ret'][$count]['min_total_score'] = '';
				$data['ret'][$count]['is_total'] = 1;
			}

			if (count($data['ret']) != 0) {

				// 总分最高分
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($_POST['end_time'] . " 23:59:59")));
				} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($_POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}
				if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				
				}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}else{
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}
				if(count($sub_role)==1 && $sub_role[0]==WebRole::Teacher[XPHEnum::Value]){
					$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
				}
				$sql = $this->sqlEscape("select * from db_attend where score=(SELECT max(score) from db_attend where  status=?? " . $where . " ) and status=?? " . $where . " group by teacher_name", array(AttendStatus::Submit[XPHEnum::Value], AttendStatus::Submit[XPHEnum::Value]));
				$max = $this->Attend_model->query($sql);

				if ($max) {
					$max_score = $max[0]['score'];
					$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
					$max_effect = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';
				} else {
					$max_effect = '';
				}


				// 总分最低分
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($_POST['end_time'] . " 23:59:59")));
				} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($_POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}
				
				if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				
				}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}else{
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}
				
				if(count($sub_role)==1 && $sub_role[0]==WebRole::Teacher[XPHEnum::Value]){
					$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
				}
				$sql = $this->sqlEscape("select * from db_attend where score=(SELECT min(score) from db_attend where  status=?? " . $where . " ) and status=?? " . $where . " group by teacher_name", array(AttendStatus::Submit[XPHEnum::Value], AttendStatus::Submit[XPHEnum::Value]));
				$max = $this->Attend_model->query($sql);
				if ($max) {
					$max_score = $max[0]['score'];
					$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
					$min_effect = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';
				} else {
					$min_effect = '';
				}

				$data['ret'][$count]['max_total_score'] = $max_effect;
				$data['ret'][$count]['min_total_score'] = $min_effect;
			}


			echo json_encode($data);
		} else {
			if ($_POST['type'] == 1) {
				foreach ($data['ret'] as $k => $v) {
					$data['ret'][$k]['attend_id'] = $k + 1;
				}
			} else {
				foreach ($data['ret'] as $k => $v) {
					$data['ret'][$k]['teacher_code'] = $k + 1;
				}
			}
			// var_dump($data['ret']);exit;
			// 合计
			$attend_num_unit = 0;
			$attend_count_unit = 0;
			$attend_num_school = 0;
			$attend_count_school = 0;

			$attend_num_outside = 0;
			$attend_count_outside = 0;

			$attend_num_teacher = 0;
			$attend_count_teacher = 0;

			$attend_num_unitLeader = 0;
			$attend_count_unitLeader = 0;

			$attend_num_majorUnitLeader = 0;
			$attend_count_majorUnitLeader = 0;

			$avg_total_score = 0; //总平均分
			$avg_school_score = 0; //校督导平均分
			$avg_unit_score = 0; //二级督导平均分
			$avg_outside_score = 0; //外聘平均分
			$avg_teacher_score = 0; //教师平均分
			$avg_unitLeader_score = 0;//领导干部
			$avg_majorUnitLeader_score = 0;//领导干部

			foreach ($data['ret'] as $k => $v) {
				$attend_num_unit += $v['unitsupervisor_num'];
				$attend_count_unit += $v['unitsupervisor_count'];
				$attend_num_school += $v['schoolsupervisor_num'];
				$attend_count_school += $v['schoolsupervisor_count'];
				$attend_num_outside += $v['outsidesupervisor_num'];
				$attend_count_outside += $v['outsidesupervisor_count'];

				$attend_num_teacher += $v['teachersupervisor_num'];
				$attend_count_teacher += $v['teachersupervisor_count'];

				$attend_num_unitLeader += $v['unitLeadersupervisor_num'];
				$attend_count_unitLeader += $v['unitLeadersupervisor_count'];

				$attend_num_majorUnitLeader += $v['majorUnitLeadersupervisor_num'];
				$attend_count_majorUnitLeader += $v['majorUnitLeadersupervisor_count'];

				$avg_total_score += $v['avg_total_score'];
				$avg_school_score += $v['avg_school_score'];
				$avg_unit_score += $v['avg_unit_score'];
				$avg_outside_score += $v['avg_outside_score'];
				$avg_teacher_score += $v['avg_teacher_score'];				
				$avg_unitLeader_score += $v['avg_unitLeader_score'];
				$avg_majorUnitLeader_score += $v['avg_majorUnitLeader_score'];
			}

			$count = count($data['ret']);

				$avg_total_score = round(($avg_total_score/$count),2);
				$avg_school_score = round(($avg_school_score/$count),2);
				$avg_unit_score = round(($avg_unit_score/$count),2);
				$avg_outside_score =  round(($avg_outside_score/$count),2);
				$avg_teacher_score=  round(($avg_teacher_score/$count),2);
				$avg_unitLeader_score =  round(($avg_unitLeader_score/$count),2);
				$avg_majorUnitLeader_score =  round(($avg_majorUnitLeader_score/$count),2);
				

			if ($_POST['type'] == 1) {
				$group1= 'attend_id';
				$group2= 'course_units';
			} else {
				$group1 = 'teacher_code';
				$group2 = 'teacher_name';
			}
			if (count($data['ret']) != 0) {

				// 总分最高分
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($_POST['end_time'] . " 23:59:59")));
				} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($_POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}
				if(count($sub_role)==1 && $sub_role[0]==WebRole::Teacher[XPHEnum::Value]){
					$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
				}
				if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				
				}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}else{
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}
				$sql = $this->sqlEscape("select * from db_attend where score=(SELECT max(score) from db_attend where  status=?? " . $where . " ) and status=?? " . $where . " group by teacher_name", array(AttendStatus::Submit[XPHEnum::Value], AttendStatus::Submit[XPHEnum::Value]));
				
				$max = $this->Attend_model->query($sql);
				if($max){
					$max_score = $max[0]['score'];
					$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
					$max_effect = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';
				}else{
					$max_effect='';
				}
				

				// 总分最低分
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($_POST['end_time'] . " 23:59:59")));
				} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($_POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($_POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}
				if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
				}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and db_attend.course_units=??',array(str_replace('分', '学', getSess()['dept_name'])));
				
				}else if(in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)){
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}else{
					$where.=$this->sqlEscape(' and teacher_code=??',array(getSess()['usercode']));
				}
				if(count($sub_role)==1 && $sub_role[0]==WebRole::Teacher[XPHEnum::Value]){
					$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
				}
				$sql = $this->sqlEscape("select * from db_attend where score=(SELECT min(score) from db_attend where  status=?? " . $where . " ) and status=?? " . $where . " group by teacher_name", array(AttendStatus::Submit[XPHEnum::Value], AttendStatus::Submit[XPHEnum::Value]));
				$max = $this->Attend_model->query($sql);
				if($max){
					$max_score = $max[0]['score'];
					$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
					$min_effect = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';
				}else{
					$min_effect='';
				}


				array_push($data['ret'], array($group1 => '合计', $group2 => '', 'unitsupervisor_num' => $attend_num_unit, 'unitsupervisor_count' => $attend_count_unit, 'schoolsupervisor_num' => $attend_num_school, 'schoolsupervisor_count' => $attend_count_school, 'outsidesupervisor_num' => $attend_num_outside, 'outsidesupervisor_count' => $attend_count_outside, 'teachersupervisor_num' => $attend_num_teacher, 'teachersupervisor_count' => $attend_count_teacher,'unitLeadersupervisor_num'=>$attend_num_unitLeader,'unitLeadersupervisor_count' =>$attend_count_unitLeader ,'majorUnitLeadersupervisor_num'=>$attend_num_majorUnitLeader,'majorUnitLeadersupervisor_count'=>$attend_count_majorUnitLeader,'avg_total_score' => $avg_total_score, 'avg_school_score' => $avg_school_score, 'avg_unit_score' => $avg_unit_score, 'avg_outside_score' => $avg_outside_score, 'avg_teacher_score' => $avg_teacher_score, 'avg_unitLeader_score'=>$avg_unitLeader_score,'avg_majorUnitLeader_score'=>$avg_majorUnitLeader_score,'max_total_score' => $max_effect, 'min_total_score' => $min_effect));

				// if(isset($_POST['type'] ) && $_POST['type']==1){
				// 	array_push($data['ret'],array('title'=>'合计',''=>'','attend_num_unit'=>$attend_num_unit,'attend_count_unit'=>$attend_count_unit,'attend_num_school'=>$attend_num_school,'attend_count_school'=>$attend_count_school,'max_effect'=>$max_effect,'min_effect'=>$min_effect));
				// }

				// if(isset($_POST['type'] ) && $_POST['type']==2){
				// 	array_push($data['ret'],array('title'=>'合计',''=>'','attend_num_unit'=>$attend_num_unit,'attend_count_unit'=>$attend_count_unit,'attend_num_school'=>$attend_num_school,'attend_count_school'=>$attend_count_school,'attend_num_outside'=>$attend_num_outside,'attend_count_outside'=>$attend_count_outside,'avg_total_score'=>$avg_total_score,'avg_school_score'=>$avg_school_score,'avg_unit_score'=>$avg_unit_score,'avg_outside_score'=>$avg_outside_score,'max_effect'=>$max_effect,'min_effect'=>$min_effect));
				// }


			} else {
				array_push($data['ret'], array($group1 => '合计', $group2 => '', 'unitsupervisor_num' => 0, 'unitsupervisor_count' => 0, 'schoolsupervisor_num' => 0, 'teachersupervisor_num' => 0, 'teachersupervisor_count' => 0, 'outsidesupervisor_count' => 0, 'teachersupervisor_num' => 0, 'teachersupervisor_count' => 0,'unitLeadersupervisor_num'=>0,'unitLeadersupervisor_count' =>0,'majorUnitLeadersupervisor_num'=>0,'majorUnitLeadersupervisor_count'=>0, 'avg_total_score' => 0, 'avg_school_score' => 0, 'avg_unit_score' => 0, 'avg_outside_score' => 0, 'avg_teacher_score' => 0,  'avg_unitLeader_score'=>0,'avg_majorUnitLeader_score'=>0,'max_total_score' => 0, 'min_total_score' => 0));

				// array_push($data['ret'],array('title'=>'合计',''=>'','attend_num_unit'=>0,'attend_count_unit'=>0,'attend_num_school'=>0,'attend_count_school'=>0,'avg_level_count'=>0,'avg_effect_count'=>0,'abovenine_level_count'=>0,'aboveeight_level_count'=>0,'abovenine_effect_count'=>0,'aboveeight_effect_count'=>0,'max_level'=>0,'min_level'=>0,'max_effect'=>0,'min_effect'=>0));
				// if(isset($_POST['type'] ) && $_POST['type']==1){
				// 	array_push($data['ret'],array('title'=>'合计',''=>'','attend_num_unit'=>0,'attend_count_unit'=>0,'attend_num_school'=>0,'attend_count_school'=>0,'max_effect'=>0,'min_effect'=>0));
				// } 
				// if(isset($_POST['type'] ) && $_POST['type']==2){
				// 	array_push($data['ret'],array('title'=>'合计',''=>'','attend_num_unit'=>0,'attend_count_unit'=>0,'attend_num_school'=>0,'attend_count_school'=>0,'attend_num_outside'=>0,'attend_count_outside'=>0,'avg_total_score'=>0,'avg_school_score'=>0,'avg_unit_score'=>0,'avg_outside_score'=>0,'max_effect'=>0,'min_effect'=>0));
				// } 


			}

			$this->load->library('PHPExcel/PHPExcel');
			$excel = new PHPExcel();
			//公共样式
			$excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
			$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

			$x_line = 0;
			$y_line = 1;

			//标题
			// $title=array($_POST['start_time'].'至'.$_POST['end_time'],'被听人数','被听人次','分院督导听课人数','分院督导听课次数','校督导听课人数','校督导听课次数','平均分','90分(含)以上','80分以下','最高分','最低分');
			if ($_POST['type'] == 1) {
				$group = '开课单位';
				$group1= 'attend_id';
				$group2= 'course_units';
			} else {
				$group = '教师姓名';
				$group1 = 'teacher_code';
				$group2 = 'teacher_name';
			}

			if(in_array(WebRole::Admin[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)||in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)||in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)||in_array(WebRole::StudentInfoOfficer[XPHEnum::Value], $role)||in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value], $role)||in_array(WebRole::UnitLeader[XPHEnum::Value], $role)||in_array(WebRole::SupervisorDirector[XPHEnum::Value], $role)||in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)||in_array(WebRole::Manage[XPHEnum::Value], $role)||in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)||in_array(WebRole::MajorUnitLeader[XPHEnum::Value], $role)){
				$title = array($_POST['start_time'] . '至' . $_POST['end_time'], $group, '被二级督导听课人数', '被二级督导听课次数', '被校督导听课人数', '被校督导听课次数', '被外聘督导听课人数', '被外聘督导听课次数','被领导听课人数', '被领导听课次数','被专业负责人听课人数', '被专业负责人听课次数',  '总分平均分', '校督导平均分', '二级督导平均分', '外聘督导平均分', '领导干部平均分', '专业负责人平均分');
				$items=array($group1, $group2, 'unitsupervisor_num', 'unitsupervisor_count', 'schoolsupervisor_num', 'schoolsupervisor_count', 'outsidesupervisor_num', 'outsidesupervisor_count', 'unitLeadersupervisor_num', 'unitLeadersupervisor_count','majorUnitLeadersupervisor_num','majorUnitLeadersupervisor_count', 'avg_total_score', 'avg_school_score', 'avg_unit_score', 'avg_outside_score', 'avg_unitLeader_score','avg_majorUnitLeader_score');

			}else{
				$title = array($_POST['start_time'] . '至' . $_POST['end_time'], $group, '被教师听课人数', '被教师听课次数',  '教师平均分', '总分最高分', '总分最低分');
				$items=array($group1, $group2, 'teachersupervisor_num', 'teachersupervisor_count', 'avg_teacher_score', 'max_total_score', 'min_total_score');
			}	

			

			// if(isset($_POST['type'] ) && $_POST['type']==1){
			// 	$title=array($_POST['start_time'].'至'.$_POST['end_time'],$group,'督导听课人数','督导听课次数','校督导听课人数','校督导听课次数','总分最高分','总分最低分');
			// }else if(isset($_POST['type'] ) && $_POST['type']==2){
			// 	$title=array($_POST['start_time'].'至'.$_POST['end_time'],$group,'督导听课人数','督导听课次数','校督导听课人数','校督导听课次数','外聘督导听课人数','外聘督导听课次数','总分平均分','校督导平均分','二级督导平均分','外聘督导平均分','总分最高分','总分最低分');
			// }

			foreach ($title as $k => $v) {
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v);
				$excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line)->getFont()->setBold(true);
				$x_line++;
			}

			$x_line = 0;
			$y_line = 2;
			//正式数据

	    	// pri($data['ret']);
			foreach ($data['ret'] as $k => $v) {
				foreach ($v as $k1 => $v1) {
					// if($k1=='id'){
					//  $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$_POST['start_time'].'至'.$_POST['end_time']);
					// }else{
					if ($k1 == 'id') {
						continue;
					}
					if(in_array($k1,$items)){
						$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line) . $y_line, $v1);
						$x_line++;
					}

				}
				$x_line = 0;
				$y_line++;
			}
			$write = new PHPExcel_Writer_Excel2007($excel);
			$director = "upload/export/" . date('Ymd', time()) . "/";
			if (!file_exists($director)) {
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($director, 0777);
			}
			$file_name = '听课数据分析表' . date('Ymd') . '.xlsx';
			$write->save($director . iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址', true, $director . $file_name));
		}
	}


	// 获取被听课教师列表
	/**
	 * @param course_units 开课单位
	 * @param start_time 开始时间
	 * @param end_time 结束时间
	 */
	function getTeacherList($select, $type, $course_units, $source, $start_time = false, $end_time = false, $isExport = false)
	{

		if ($isExport) { //是否导出
			$select = $_POST['select'];
			$type = $_POST['type'];
			$course_units = $_POST['course_units'];
			$start_time = $_POST['start_time'];
			$end_time = $_POST['end_time'];
		}

		$course_units = urldecode($course_units);

		if ($start_time && !is_numeric($start_time)) {
			$start_time = strtotime(urldecode($start_time));
		}
		if ($end_time  && !is_numeric($end_time)) {
			$end_time = strtotime(urldecode($end_time));
		}
		$role = explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);	

				// pri('数据分析1');
		if ($select == '1') {
  	       $connar['where'] = $this->sqlEscape("db_attend.status=?? and db_attend.course_units=?? ", array(AttendStatus::Submit[XPHEnum::Value], $course_units));
			$connar['join'] = array('db_admin', 'db_admin.id=db_attend.listen_id');
			$connar['orderby'] = "CONVERT(teacher_name using gbk);";
			if ($type == 1) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Unit[XPHEnum::Value]));
			} else if ($type == 2) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::School[XPHEnum::Value]));
			}else if ($type == 3) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Outside[XPHEnum::Value]));
			}else if ($type == 4) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Teacher[XPHEnum::Value]));
			} else if ($type == 5) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::UnitLeader[XPHEnum::Value]));
			} else if ($type == 6) {
				$connar['items'] = "teacher_name,teacher_code,score,listen_name,db_admin.usercode listen_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::MajorUnitLeader[XPHEnum::Value]));
			} 

			if (in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)) {//管理员
				// 记得解开
				// $connar['where'].=$this->sqlEscape(' and admin_type=??',array(AdminType::Auth[XPHEnum::Value]));
			} else if (in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)) {//二级教学单位督导组长
				if (!in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) && !in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) && !in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role) && !in_array(WebRole::UnitLeader[XPHEnum::Value], $role)){
					$connar['where'] .= $this->sqlEscape(' and db_attend.course_units=??', array(getSess()['dept_name']));
				}		
			} else if (in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role) || in_array(WebRole::UnitLeader[XPHEnum::Value], $role) || in_array(WebRole::Teacher[XPHEnum::Value], $role)) {
				$connar['where'] .= $this->sqlEscape(' and teacher_code=??', array(getSess()['usercode']));
			}
		} else {
			$connar['where'] = $this->sqlEscape("db_attend.status=?? and teacher_code=?? ", array(AttendStatus::Submit[XPHEnum::Value], $course_units));
			$connar['orderby'] = "listen_id";
			$connar['join'] = array('db_admin', 'db_admin.id=db_attend.listen_id');
			$connar['items'] = "listen_name ,usercode as teacher_code";
			if ($type == 1) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Unit[XPHEnum::Value]));
			} else if ($type == 2) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::School[XPHEnum::Value]));
			}else if ($type ==3) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Outside[XPHEnum::Value]));
			}else if ($type == 4) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::Teacher[XPHEnum::Value]));
			}else if ($type == 5) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::UnitLeader[XPHEnum::Value]));
			}else if ($type == 6) {
				$connar['items'] = "listen_name ,usercode as listen_code,db_attend.score score,teacher_name,teacher_code";
				$connar['where'] .= $this->sqlEscape("and type=??", array(AttendType::MajorUnitLeader[XPHEnum::Value]));
			}
			//  else if ($type == 5) {
			// 	$connar['where'] .= $this->sqlEscape("and teaching_level_score>=90", array());
			// } else if ($type == 6) {
			// 	$connar['where'] .= $this->sqlEscape("and teaching_level_score<80", array());
			// } else if ($type == 7) {
			// 	$connar['where'] .= $this->sqlEscape("and teaching_effect_score>=90", array());
			// } else if ($type == 8) {
			// 	$connar['where'] .= $this->sqlEscape("and teaching_effect_score<80", array());
			// }
		}


		// $connar['join']=array('db_admin','db_admin.id=db_')

		// if (isset($params['start_time']) && isset($params['end_time'])) {
		// 	$where.=$this->sqlEscape(' and db_attend.listen_time>??  and db_attend.listen_time<??',array(strtotime($params['start_time'].' 00:00:00'),strtotime($_POST['end_time']." 23:59:59")));
		// }else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
		// 	$where.=' and db_attend.listen_time>'.strtotime($_POST['start_time'].' 00:00:00').' and db_attend.listen_time<'.strtotime($_POST['end_time']." 23:59:59");
		// }
		if ($start_time) {
			$connar['where'] .= $this->sqlEscape(' and listen_time>??', array($start_time . ' 00:00:00'));
		}
		if ($end_time) {
			$connar['where'] .= $this->sqlEscape(' and listen_time<??', array($end_time . " 23:59:59"));
		}
		$data['list'] = $this->Attend_model->get_list_full($connar);

		// echo $this->db->last_query();

		$data['newlist']=array();
		foreach ($data['list'] as $k => $v) {
			$data['newlist'][$v['teacher_code']][]=$v;
		}

		if ($type == 1) {
			$data['title'] = "被二级督导听课次数";
		} else if ($type == 2) {
			$data['title'] = "被校督导听课次数";
		}else if ($type == 3) {
			$data['title'] = "被外聘督导听课次数";
		}else if ($type == 4) {
			$data['title'] = "被教师听课次数";
		}else if ($type == 5) {
			$data['title'] = "被领导干部听课次数";
		}else if ($type == 6) {
			$data['title'] = "被专业负责人听课次数";
		}
		//  else if ($type == 5) {
		// 	$data['title'] = "教学水平90分(含)以上";
		// } else if ($type == 6) {
		// 	$data['title'] = "教学水平80以下";
		// } else if ($type == 7) {
		// 	$data['title'] = "教学效果90分(含)以上";
		// } else if ($type == 8) {
		// 	$data['title'] = "教学效果80以下";
		// }

		//导出
		if ($isExport) {

			$this->load->library('PHPExcel/PHPExcel');
			$excel = new PHPExcel();
			//公共样式
			$excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
			$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
			$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

			$x_line = 0;
			$y_line = 1;

			//正式开始
			$excel = new PHPExcel();
			$excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);

			//标题
			$excel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex(0) . '1:' . PHPExcel_Cell::stringFromColumnIndex(4) . '1');
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0) . '1', $data['title']);
			$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
			$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
			$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$i = 2;
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0) . $i, '序号');
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(1) . $i, '授课教师');
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(2) . $i, '分数');
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(3) . $i, '听课教师');
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(4) . $i, '平均分');

			$i++;
			foreach ($data['newlist'] as $k => $v) {
				$start_i=$i;
	           foreach ($v as $k1 => $v1) {
				$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0).$i,$i-2);
	            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(1).$i,$v1['teacher_code']."(".$v1['teacher_name'].")");
	            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(2).$i,$v1['score']); 
	            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(3).$i,$v1['listen_name']); 
	            $excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(4).$i,sprintf("%.2f",array_sum(array_column($v,'score'))/count($v)));           
	            $i++;
			   }
			   
			   $excel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex(1).$start_i.':'.PHPExcel_Cell::stringFromColumnIndex(1).($i-1));
			   $excel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex(4).$start_i.':'.PHPExcel_Cell::stringFromColumnIndex(4).($i-1));
	        }

			//设置对齐
			$excel->getActiveSheet()->getStyle('A1:' . PHPExcel_Cell::stringFromColumnIndex(9) . ($i - 1))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$excel->getActiveSheet()->getStyle('A1:' . PHPExcel_Cell::stringFromColumnIndex(0) . ($i - 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


			$write = new PHPExcel_Writer_Excel2007($excel);
			$director = "upload/export/" . date('Ymd', time()) . "/";
			if (!file_exists($director)) {
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($director, 0777);
			}
			$file_name = $data['title'] . date('Ymd') . '.xlsx';
			$write->save($director . iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址', true, $director . $file_name));
		} else {
			$data['params'] = array('select' => $select, 'source' => $source, 'type' => $type, 'course_units' => $course_units,'listen_type'=>'', 'start_time' => $start_time, 'end_time' => $end_time);
			$this->load->view('admin/attendDept', $data);
		}
	}


	//获取共同部分
	function getConType($datalist,$type,$params,$POST){
			foreach ($datalist as $k => $v) {
				
				//开课单位
				if($type == 1){
					$role = explode(',', getSess()['sub_role_id']);
					array_push($role, getSess()['role_id']);

					if (in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)) {
						$where_role = ' and 1=1';
					} else {
						$where_role = $this->sqlEscape(' and teacher_code=?? ', array(getSess()['usercode']));
					}
					$course_units = $v['course_units'];
					$data['ret'][$k]['attend_id'] =$v['attend_id'];
					$data['ret'][$k]['course_units'] = $course_units;
				}else{		
					$data['ret'][$k]['teacher_code'] =$v['teacher_code'];
					$data['ret'][$k]['teacher_name'] =$v['teacher_name'];
				}

				// 被分院督导听课人数 --本分院
				if($type == 1){
					$connar2['items'] = 'count(DISTINCT teacher_code) unitsupervisor_num';
					$connar2['where'] = $this->sqlEscape(
						'db_attend.course_units=?? and db_attend.status=?? and type=??',
						array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value])
					);
					$connar2['where'] .= $where_role;
				}else{
					$connar2['items'] = 'count(DISTINCT listen_name) unitsupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value]));
 			    }
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
	
				$unitsupervisor_num = $this->Attend_model->get_single_full($connar2);

				// echo $this->db->last_query();
				$data['ret'][$k]['unitsupervisor_num'] = $unitsupervisor_num['unitsupervisor_num'];


				// 被分院督导听课次数 
				$connar2['items'] = 'COUNT(*) unitsupervisor_count';
				if($type == 1){
                  $connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value]));
                  $connar2['where'] .= $where_role;
				}else{
				  $connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value]));
				}

				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($_POST['start_time']), strtotime($_POST['end_time'] . '23:59:59')));
				} else if (isset($_POST['start_time']) && isset($_POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($_POST['start_time']), strtotime($_POST['end_time'] . '23:59:59')));
				}	
				if (isset($params['class_type']) && $params['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}

				$unitsupervisor_count = $this->Attend_model->get_single_full($connar2);
				 // echo $this->db->last_query();
				$data['ret'][$k]['unitsupervisor_count'] = $unitsupervisor_count['unitsupervisor_count'];


				// 被校督导听课人数  				
				if($type == 1){
					$connar2['items'] = 'count(DISTINCT teacher_code) schoolsupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::School[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
					$connar2['items'] = 'count(DISTINCT listen_name) schoolsupervisor_num';
				    $connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::School[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				$schoolsupervisor_num = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['schoolsupervisor_num'] = $schoolsupervisor_num['schoolsupervisor_num'];



				// 被校督导听课次数  
				$connar2['items'] = 'COUNT(*) schoolsupervisor_count';
				if($type == 1){				
				   $connar2['where'] = $this->sqlEscape('type=?? and db_attend.course_units=?? and db_attend.status=??', array(AttendType::School[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
				   $connar2['where'] .= $where_role;
				}else{
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.teacher_code=?? and db_attend.status=??', array(AttendType::School[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}			
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$schoolsupervisor_count = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['schoolsupervisor_count'] = $schoolsupervisor_count['schoolsupervisor_count'];


				// 被外聘督导听课人数  
				if($type == 1){
					$connar2['items'] = 'count(DISTINCT teacher_code) outsidesupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Outside[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
					$connar2['items'] = 'count(DISTINCT listen_name) outsidesupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Outside[XPHEnum::Value]));

				}		
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$outsidesupervisor_num = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['outsidesupervisor_num'] = $outsidesupervisor_num['outsidesupervisor_num'];


				// 被外聘督导听课次数  
				$connar2['items'] = 'COUNT(*) outsidesupervisor_count';
				if($type == 1){
				  $connar2['where'] = $this->sqlEscape('type=?? and db_attend.course_units=?? and db_attend.status=??', array(AttendType::Outside[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
				  $connar2['where'] .= $where_role;
				}else{
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.teacher_code=?? and db_attend.status=??', array(AttendType::Outside[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$outsidesupervisor_count = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['outsidesupervisor_count'] = $outsidesupervisor_count['outsidesupervisor_count'];


				// 被教师听课人数  
				if($type==1){
					$connar2['items'] = 'count(DISTINCT teacher_code) teachersupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Teacher[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
                    $connar2['items'] = 'count(DISTINCT listen_name) teachersupervisor_num';
                    $connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Teacher[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}	
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				$teachersupervisor_num = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['teachersupervisor_num'] = $teachersupervisor_num['teachersupervisor_num'];


				// 被教师听课次数  
				$connar2['items'] = 'COUNT(*) teachersupervisor_count';
				if($type == 1){
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.course_units=?? and db_attend.status=??', array(AttendType::Teacher[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.teacher_code=?? and db_attend.status=??', array(AttendType::Teacher[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$teachersupervisor_count = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['teachersupervisor_count'] = $teachersupervisor_count['teachersupervisor_count'];



				// 被领导干部听课人数  
				if($type==1){
					$connar2['items'] = 'count(DISTINCT teacher_code) unitLeadersupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::UnitLeader[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
                    $connar2['items'] = 'count(DISTINCT listen_name) unitLeadersupervisor_num';
                    $connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::UnitLeader[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}	
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				$unitLeadersupervisor_num = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['unitLeadersupervisor_num'] = $unitLeadersupervisor_num['unitLeadersupervisor_num'];


				// 被领导干部听课次数  
				$connar2['items'] = 'COUNT(*) unitLeadersupervisor_count';
				if($type == 1){
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.course_units=?? and db_attend.status=??', array(AttendType::UnitLeader[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.teacher_code=?? and db_attend.status=??', array(AttendType::UnitLeader[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$unitLeadersupervisor_count = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['unitLeadersupervisor_count'] = $unitLeadersupervisor_count['unitLeadersupervisor_count'];



				// 被专业负责人听课人数  
				if($type==1){
					$connar2['items'] = 'count(DISTINCT teacher_code) majorUnitLeadersupervisor_num';
					$connar2['where'] = $this->sqlEscape('db_attend.course_units=?? and db_attend.status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::MajorUnitLeader[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
                    $connar2['items'] = 'count(DISTINCT listen_name) majorUnitLeadersupervisor_num';
                    $connar2['where'] = $this->sqlEscape('db_attend.teacher_code=?? and db_attend.status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::MajorUnitLeader[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}	
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				$majorUnitLeadersupervisor_num = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['majorUnitLeadersupervisor_num'] = $majorUnitLeadersupervisor_num['majorUnitLeadersupervisor_num'];


				// 被专业负责人听课次数  
				$connar2['items'] = 'COUNT(*) majorUnitLeadersupervisor_count';
				if($type == 1){
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.course_units=?? and db_attend.status=??', array(AttendType::MajorUnitLeader[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
					$connar2['where'] .= $where_role;
				}else{
					$connar2['where'] = $this->sqlEscape('type=?? and db_attend.teacher_code=?? and db_attend.status=??', array(AttendType::MajorUnitLeader[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar2['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar2['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$majorUnitLeadersupervisor_count = $this->Attend_model->get_single_full($connar2);
				$data['ret'][$k]['majorUnitLeadersupervisor_count'] = $majorUnitLeadersupervisor_count['majorUnitLeadersupervisor_count'];


				//总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
					$connar1['where'] = $this->sqlEscape('course_units=?? and status=??', array($course_units, AttendStatus::Submit[XPHEnum::Value]));
					$connar1['where'] .= $where_role;
				}else{
					$connar1['where'] = $this->sqlEscape('teacher_code=?? and status=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_total_score'] = sprintf("%.2f", $avg['avg']);


				//被校督导听课总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
					$connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::School[XPHEnum::Value]));
					$connar1['where'] .= $where_role;
				}else{
					$connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::School[XPHEnum::Value]));
				}
		
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_school_score'] = sprintf("%.2f", $avg['avg']);


				//被二级督导听课总平均分
				$connar1['items'] = 'avg(score) avg';				
				if($type == 1){
					$connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value]));
					$connar1['where'] .= $where_role;
				}else{
					$connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Unit[XPHEnum::Value]));
				}
				
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_unit_score'] = sprintf("%.2f", $avg['avg']);


				//外督导听课总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
					$connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Outside[XPHEnum::Value]));
					$connar1['where'] .= $where_role;
				}else{
					$connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Outside[XPHEnum::Value]));
				}
			
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
			
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_outside_score'] = sprintf("%.2f", $avg['avg']);

				//被教师听课总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
				   $connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::Teacher[XPHEnum::Value]));
				   $connar1['where'] .= $where_role;
				}else{
				  $connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::Teacher[XPHEnum::Value]));
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				//课程类型			
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_teacher_score'] = sprintf("%.2f", $avg['avg']);


					//被领导干部听课总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
				   $connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::UnitLeader[XPHEnum::Value]));
				   $connar1['where'] .= $where_role;
				}else{
				  $connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::UnitLeader[XPHEnum::Value]));
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				//课程类型			
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_unitLeader_score'] = sprintf("%.2f", $avg['avg']);


					//被专业负责人听课总平均分
				$connar1['items'] = 'avg(score) avg';
				if($type == 1){
				   $connar1['where'] = $this->sqlEscape('course_units=?? and status=?? and type=??', array($course_units, AttendStatus::Submit[XPHEnum::Value], AttendType::MajorUnitLeader[XPHEnum::Value]));
				   $connar1['where'] .= $where_role;
				}else{
				  $connar1['where'] = $this->sqlEscape('teacher_code=?? and status=?? and type=??', array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], AttendType::MajorUnitLeader[XPHEnum::Value]));
				}
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$connar1['where'] .= $this->sqlEscape(" and listen_time>=?? and listen_time<=??", array(strtotime($POST['start_time']), strtotime($POST['end_time'] . '23:59:59')));
				}
				//课程类型			
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$connar1['where'] .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$avg = $this->Attend_model->get_single_full($connar1);
				$data['ret'][$k]['avg_majorUnitLeader_score'] = sprintf("%.2f", $avg['avg']);


				//总分最高分
				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($POST['end_time'] . " 23:59:59")));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}		
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$where .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}
				
				$role=explode(',',getSess()['sub_role_id']);
				array_push($role,getSess()['role_id']);	
				$role=array_filter($role);
				$role=array_merge($role);
				
				if($type == 1){
					if(count($role)==1 && $role[0]==WebRole::Teacher[XPHEnum::Value]){
						$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
					}
					$sql = $this->sqlEscape("select * from db_attend where score=(SELECT max(score) from db_attend where course_units=??  and status=?? " . $where . $where_role . " ) and course_units=??  and status=?? " . $where . $where_role . " group by teacher_name", array($course_units, AttendStatus::Submit[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
				}else{
					if(count($role)==1 && $role[0]==WebRole::Teacher[XPHEnum::Value]){
						$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
					}
					$sql = $this->sqlEscape("select * from db_attend where score=(SELECT max(score) from db_attend where teacher_code=??  and status=?? " . $where . " ) and teacher_code=??  and status=?? " . $where . " group by teacher_name", array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				$max = $this->Attend_model->query($sql);
				if (isset($max[0]['score'])) {
					$max_score = $max[0]['score'];
				} else {
					$max_score = 0;
				}

				$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
				$data['ret'][$k]['max_total_score'] = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';


				//总分最低分

				if (isset($params['start_time']) && isset($params['end_time'])) {
					$where = $this->sqlEscape(' and db_attend.listen_time>=??  and db_attend.listen_time<=??', array(strtotime($params['start_time'] . ' 00:00:00'), strtotime($POST['end_time'] . " 23:59:59")));
				} else if (isset($POST['start_time']) && isset($POST['end_time'])) {
					$where = ' and db_attend.listen_time>=' . strtotime($POST['start_time'] . ' 00:00:00') . ' and db_attend.listen_time<=' . strtotime($POST['end_time'] . " 23:59:59");
				} else {
					$where = '';
				}
				//课程类型			
				if (isset($POST['class_type']) && $POST['class_type'] > -1) {
					$where .= $this->sqlEscape(" and db_attend.type=?? ", array($params['class_type']));
				}

				if($type == 1){
					if(count($role)==1 && $role[0]==WebRole::Teacher[XPHEnum::Value]){
						$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
					}
					$sql = $this->sqlEscape("select * from db_attend where score=(SELECT min(score) from db_attend where course_units=??  and status=?? " . $where . $where_role . " ) and course_units=??  and status=?? " . $where . $where_role . " group by teacher_name", array($course_units, AttendStatus::Submit[XPHEnum::Value], $course_units, AttendStatus::Submit[XPHEnum::Value]));
				}else{
					if(count($role)==1 && $role[0]==WebRole::Teacher[XPHEnum::Value]){
						$where.=$this->sqlEscape(" and db_attend.type=?? ", array(AttendType::Teacher[XPHEnum::Value]));
					}
					$sql = $this->sqlEscape("select * from db_attend where score=(SELECT min(score) from db_attend where teacher_code=??  and status=?? " . $where . " ) and teacher_code=??  and status=?? " . $where . " group by teacher_name", array($v['teacher_code'], AttendStatus::Submit[XPHEnum::Value], $v['teacher_code'], AttendStatus::Submit[XPHEnum::Value]));
				}
				
				$max = $this->Attend_model->query($sql);
				if (isset($max[0]['score'])) {
					$max_score = $max[0]['score'];
				} else {
					$max_score = 0;
				}

				$max_teacher_name = implode(',', array_column($max, 'teacher_name'));
				$data['ret'][$k]['min_total_score'] = sprintf("%.2f", $max_score) . '(' . $max_teacher_name . ')';
			}

			if ($params['course_units'] == '') {
				$data['ret'] = array();
			}

			return $data['ret'];

	}
}
